package com.wn.service.impl;

import com.wn.bean.Permission;
import com.wn.bean.vo.MenuVo;
import com.wn.mapper.PermissionMapper;
import com.wn.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2024-07-09
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<MenuVo> findMenuByUserName(String username) {
        //菜单集合
        List<MenuVo> menuVos = new ArrayList<>();
        //01 拿到所有表单权限
       List<Permission> permissions = permissionMapper.findPermissionByUserName(username);
        //02 把权限转成菜单
        //Permission 转成 
        for (Permission permission : permissions) {
            //拿到外层的菜单 一级菜单
            if (permission.getParentId() == 0){
                MenuVo menuVo = new MenuVo();
                //权限的id
                menuVo.setId(permission.getId());
                menuVo.setPath(String.valueOf(permission.getSort()));
                //权限的名称
                menuVo.setTitle(permission.getPname());
                menuVo.setIcon("el-icon-user");
                //子菜单的集合
                List<MenuVo> childMenuVos = new ArrayList<>();
                //组装二级菜单
                for (Permission p : permissions) {
                     //   permission.getId() 一级菜单的id
                     //   p.getParentId()    二级级菜单的parentId
                    if(permission.getId() == p.getParentId()){
                        MenuVo m = new MenuVo();
                        m.setId(p.getId());
                        m.setPath(p.getSort().toString());
                        m.setTitle(p.getPname());
                        m.setLinkUrl(p.getUrl());
                        childMenuVos.add(m);
                    }
                }
                menuVo.setChildren(childMenuVos);
                //权限的url 一级菜单没有url 不用给
                menuVos.add(menuVo);
            }
        }
        return menuVos;
    }
}
